// 'use strict'

// let nunjucks = require('nunjucks');

// function createEnv(path, opts) {
//     path = path || "";
//     opts = opts || {};

//     let loaderOptions = {
//         watch: opts.watch || true,
//         noChche: opts.noChche || true
//     }

//     let envOptions = {
//         autoescape: opts.autoescape || true,
//         throwOnUndefined: opts.throwOnUndefined || false
//     }
//     let loader = new nunjucks.FileSystemLoader("view", loaderOptions);

//     let env = new nunjucks.Environment(loader, envOptions);

//     if (opts.files) {
//         for (let ch in opts.files) {
//             env.addFilter(ch, opts.files[ch]);
//         }
//     }
//     return env;
// }

// module.exports = async (ctx, next) => {
//     ctx.render = function (view, model) {
//         let env = createEnv();
//         ctx.body = env.render(view, model);
//     }
//     await next();
// }


'use strict';

let nunjucks = require("nunjucks");

function createEnv(path,opts){
    path = path || "";
    opts = opts || {};

    let envOptions = {
        watch:opts.watch === undefined? true : opts.watch,
        autoescape: opts.autoescape === undefined ? true : opts.autoescape,
        noCach: opts.noCach === undefined ? true : opts.noCach,
        throwOnUndefind: opts.throwOnUndefind === undefined ? true : opts.throwOnUndefind,
    }

    let env = nunjucks.configure(path,envOptions)
    return env;
}

module.exports = async(ctx,next)=>{
    ctx.render = function(view,modle){
        let env = createEnv("view",{autoescape:false});
        ctx.body = env.render(view,modle);
    }
    await next();
}
